param (
    [string]$SDK_PATH = "E:\OpenHarmony\Sdk\15",
    [ValidateSet('arm', 'aarch64', 'x86_64')]
    [string]$ARCH = "arm"
)

# D:\Huawei\Openharmony\Sdk\native-windows-x64-6.0.0.38-Canary1
# E:\OpenHarmony\Sdk\15

# Set OpenHarmony SDK path
$OH_SDK_HOME = $SDK_PATH
$CLANG = "$OH_SDK_HOME/native/llvm/bin/clang++.exe"
$SYSROOT = "$OH_SDK_HOME/native/sysroot"
$TARGET = "arm-linux-ohos"
$GCC_TOOLCHAIN = "$OH_SDK_HOME/native/llvm"

$build_dir = "$PSScriptRoot/build"
$release_dir = "$PSScriptRoot/release"
$exePath = "$release_dir/ohos"
# Define the array of directories to clean and create
$directories = @($release_dir, $build_dir)

# Clean directories
foreach ($dir in $directories) {
    if (Test-Path $dir) {
        Remove-Item $dir -Recurse -Force -ErrorAction Stop
    }
}

# Create directories
foreach ($dir in $directories) {
    New-Item -Path $dir -ItemType Directory -Force | Out-Null
}
# Set target platform according to architecture
switch ($ARCH) {
    "arm" { $TARGET = "arm-linux-ohos" }
    "aarch64" { $TARGET = "aarch64-linux-ohos" }
    "x86_64" { $TARGET = "x86_64-linux-ohos" }
}

Write-Host "Building for architecture: $ARCH"
Write-Host "Using OpenHarmony SDK: $OH_SDK_HOME"

# Add library paths
$LIB_PATHS = @(
    "-L`"$SYSROOT/usr/lib`"",
    "-L`"$SYSROOT/usr/lib/$TARGET`"",
    "-L`"$OH_SDK_HOME/native/share/libs`"",
    "-L`"$OH_SDK_HOME/native/share/libs/multimedia`""
)

# Compile parameters
$INCLUDES = @(
    "-I`"$SYSROOT/usr/include`"",
    "-I`"$OH_SDK_HOME/native/share/include`"",
    "-I`"$PSScriptRoot/include`""
)

$DEFINES = @(
    "-DPORT=12345"
)

$COMPILE_FLAGS = @(
    "--target=$TARGET",
    "--sysroot=$SYSROOT",
    "-fPIC",
    "-Wall"
)

# Link parameters
$LINK_FLAGS = @(
    "--target=$TARGET",
    "--sysroot=$SYSROOT",
    "-fPIC",
    "-Wall",
    "-lnative_avscreen_capture",
    "-lnative_buffer",
    "-lnative_media_core",
    "-lnative_media_avcencinfo",
    "-lnative_media_venc",
    "-lnative_media_codecbase",
    "-lhilog_ndk.z",
    "-ldeviceinfo_ndk.z",
    "-lnative_display_manager"
)

# Check if library files exist
Write-Host "Checking library paths..."
foreach ($path in $LIB_PATHS) {
    $libPath = $path -replace "^-L`"(.+)`"$", '$1'
    if (Test-Path $libPath) {
        Write-Host "Found library path: $libPath"
        Get-ChildItem $libPath -Filter "*multimedia*.so" | ForEach-Object {
            Write-Host "  Found library: $($_.Name)"
        }
    } else {
        Write-Host "Warning: Library path not found: $libPath"
    }
}


# Compile source file
Write-Host "Compiling $exePath ..."

# Find all cpp files in src, excluding AndroidDevice.cpp
$cppFiles = Get-ChildItem "$PSScriptRoot/src" -Filter *.cpp | Where-Object { $_.Name -ne "AndroidDevice.cpp" } | ForEach-Object { $_.FullName }
$objFiles = @()
foreach ($cpp in $cppFiles) {
    $obj = Join-Path $build_dir ( [System.IO.Path]::GetFileNameWithoutExtension($cpp) + ".o" )
    $CompileArgs = $COMPILE_FLAGS + $DEFINES + $INCLUDES + @("-c", $cpp, "-o", $obj)
    & $CLANG $CompileArgs
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Compilation failed for $cpp!"
        exit 1
    }
    $objFiles += $obj
}

# Link all object files
Write-Host "Linking executable..."
$LinkArgs = $LINK_FLAGS + $objFiles + $LIB_PATHS + @("-o", $exePath, "-v")
Write-Host "Link command: $CLANG $LinkArgs"
& $CLANG $LinkArgs

if ($LASTEXITCODE -ne 0) {
    Write-Error "Linking failed!"
    exit 1
}

& "$OH_SDK_HOME/native/llvm/bin/llvm-strip.exe" $exePath

Write-Host "Build completed successfully!" -ForegroundColor Green